#include <bits/stdc++.h>

#define eb emplace_back
#define ep emplace
#define fi first
#define se second
#define in read<int>()
#define lin read<ll>()
#define rep(i, x, y) for(int i = (x); i <= (y); i++)
#define per(i, x, y) for(int i = (x); i >= (y); i--)

using namespace std;

using ll = long long;
using db = double;
using pii = pair < int, int >;
using vec = vector < int >;
using veg = vector < pii >;

template < typename T > T read() {
	T x = 0; bool f = 0; char ch = getchar();
	while(!isdigit(ch)) f |= ch == '-', ch = getchar();
	while(isdigit(ch)) x = x * 10 + (ch ^ 48), ch = getchar();
	return f ? -x : x;
}

template < typename T > void chkmax(T &x, const T &y) { x = x > y ? x : y; }
template < typename T > void chkmin(T &x, const T &y) { x = x < y ? x : y; }

const int N = 3e5 + 10;

char s[N], t[N];
int ans = 0, n, m;

int main() {
#ifndef ONLINE_JUDGE
	freopen("1.in", "r", stdin);
#endif
	n = in, m = in; scanf("%s", s + 1); scanf("%s", t + 1);
	rep(_, 0, 1) {
		if(_ != s[1] - '0' && _ != t[1] - '0') continue;
		int cnt = 0, s0 = 0, t0 = 0, v0 = 0, cur = _;
		rep(i, 1, n + m) {
			if(s[i] == '0') s0++; if(t[i] == '0') t0++;
			if(cur == 0) {
				if(v0 + 1 <= max(s0, t0)) cnt++; else cur = 1;
			} else {
				if(v0 >= min(s0, t0)) cnt++; else cur = 0;
			}
			if(cur == 0) v0++;
		}
		chkmax(ans, cnt);
	} cout << ans - 1 << endl;
	return 0;
}
